<template>
  <div class="confirm-box-container" :style="{ zIndex: zIndex }" @click.stop="">
    <div class="confirm-box">
      <div class="confirm-box-header">
        <div class="title">{{ title }}</div>
        <div class="close" @click="closeHandle" @mouseover="color = '#994ae7'" @mouseout="color = '#9FA2AC'">
          <svgIcon name="close" :color="color" />
        </div>
      </div>
      <div class="confirm-box-body">{{ message }}</div>
      <!-- 底部 -->
      <div class="confirm-box-footer">
        <div class="dialog-footer">
          <div class="app-button-info cancel" @click="cancelHandle">{{ cancelButtonText }}</div>
          <div class="app-button-primary confirm" @click="confirmHandle">{{ confirmButtonText }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onBeforeUnmount } from "vue";
const props = defineProps({
  zIndex: {
    type: Number,
    default: 1000
  },
  title: {
    type: String,
    default: "提示"
  },
  message: {
    type: String,
    default: ""
  },
  // 是否显示取消按钮
  showCancelButton: {
    type: Boolean,
    default: true
  },
  // 是否显示确定按钮
  showConfirmButton: {
    type: Boolean,
    default: true
  },
  // 是否显示取消按钮
  cancelButtonText: {
    type: String,
    default: "取消"
  },
  // 确定按钮的文本内容
  confirmButtonText: {
    type: String,
    default: "确定"
  },
  // 销毁
  destroy: {
    type: Function,
    default: () => {}
  },
  // 确认框
  confirm: {
    type: Function,
    default: () => {}
  },
  // 取消
  cancel: {
    type: Function,
    default: () => {}
  },
  // 关闭前
  beforeClose: {
    type: Function,
    default: null
  }
});
const color = ref("#9FA2AC");
// 确认
function confirmHandle() {
  props.confirm();
  if (props.beforeClose) {
    props.beforeClose("confirm", props.destroy);
  } else {
    props.destroy();
  }
}
// 取消
function cancelHandle() {
  props.cancel();
  if (props.beforeClose) {
    props.beforeClose("cancel", props.destroy);
  } else {
    props.destroy();
  }
}
// 关闭
function closeHandle() {
  if (props.beforeClose) {
    props.beforeClose("close", props.destroy);
  } else {
    props.destroy();
  }
}
onBeforeUnmount(() => {
  console.log("组件销毁前");
});
</script>

<style scoped lang="scss">
.confirm-box-container {
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
  background: rgb(0 0 0 / 50%);
}
.confirm-box {
  position: relative;
  width: 360px;
  min-height: 260px;
  padding: 24px;
  padding-top: 32px;
  padding-bottom: 64px;
  background: #ffffff;
  background-image: url("@/assets/background/dialog_bg.png");
  background-repeat: no-repeat;
  background-size: 100% 160px;
  border-radius: 24px;
}
.confirm-box-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 130px;
  padding: 24px 0;
  font-size: 14px;
  font-weight: 400;
  line-height: 24px;
  color: #717681;
}
.confirm-box-header {
  position: relative;
  height: 24px;
  text-align: center;
  .title {
    height: 24px;
    font-size: 18px;
    font-weight: 400;
    line-height: 24px;
    color: #292b30;
    text-align: center;
  }
  .close {
    position: absolute;
    top: 4px;
    right: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    font-size: 20px;
    color: #9fa2ac;
    cursor: pointer;
    transition: all 0.3s;
    &:hover {
      color: #994ae7;
      transform: rotate(180deg);
    }
  }
}
.confirm-box-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 0 24px 24px;
}
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .cancel {
    width: 148px;
  }
  .confirm {
    width: 148px;
  }
}
</style>
